package cn.huihuo.weibo.controller;

import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.module.Product;
import cn.huihuo.weibo.service.MessageService;
import cn.huihuo.weibo.service.ParticipationService;
import cn.jfinalbbs.common.BaseController;
import cn.jfinalbbs.common.Constants;
import cn.jfinalbbs.user.User;
import cn.jfinalbbs.utils.EncryptionUtil;
import cn.jfinalbbs.utils.StrUtil;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Dan
 * Created by zhengjunbo on 2015/9/11.
 */
public class IndexProductController extends BaseController{

    public void index(){
        User user = getSessionAttr(Constants.USER_SESSION);

        setAttr("messageSize", user==null?"":MessageService.service.total(user.getStr("id")));
        setAttr("participationSize", user==null?"": ParticipationService.service.findTotalParticipation(user.getStr("id")));
        render("huihuo/index.html");
//        Product.product.paginate(10,20,"select * from product","all");
    }


    public void qqlogincallback(){
        System.out.println(getPara());
        renderNull();
    }




    public void saveProduct(){
        String mainId = getPara("mainId");
        System.out.println(mainId);
        redirect(Constants.getBaseUrl() + "/");
    }

    public void loginRedirect(){
        render("huihuo/login.html");
    }

    //本地登录
    public void login() {
        String method = getRequest().getMethod();
        if(method.equalsIgnoreCase(Constants.RequestMethod.GET)) {
            String email = getCookie(Constants.COOKIE_EMAIL);
            setAttr(Constants.COOKIE_EMAIL, email);
            render("front/user/login.html");
        } else if(method.equalsIgnoreCase(Constants.RequestMethod.POST)) {
            String email = getPara("email");
            String password = getPara("password");
            User user = User.me.localLogin(email, EncryptionUtil.md5(password));
            if(user == null) {
                error("邮箱或密码错误");
            } else {
                // 记住邮箱
                String remember_me = getPara("remember_me");
                if("1".equalsIgnoreCase(remember_me)) {
                    setCookie(Constants.COOKIE_EMAIL, email, 60*60*24*30);
                }
                setCookie(Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30*24*60*60);
                setSessionAttr(Constants.USER_SESSION, user);
                success();
            }
        }
    }


    //本地注册
    public void register() {
        String method = getRequest().getMethod();
        if(method.equalsIgnoreCase(Constants.RequestMethod.GET)) {
            render("front/user/register.html");
        } else if(method.equalsIgnoreCase(Constants.RequestMethod.POST)) {
            String nickname = getPara("nickname");
            String gender = getPara("gender");
            String password = getPara("password");
            String email = getPara("email");
            if(User.me.findByEmail(email) == null) {
                User user = new User();
                user.set("id", StrUtil.getUUID())
                        .set("nickname", nickname)
                        .set("token", StrUtil.getUUID())
                        .set("gender", gender)
                        .set("score", 0)
                        .set("expire_time",new Date())
                        .set("mission", new Date())
                        .set("open_id","")
                        .set("in_time", new Date())
                        .set("email", email)
                        .set("thirdlogin_type","normal")
                        .set("avatar", Constants.getBaseUrl() + "/static/img/default_avatar.png")
                        .set("password", EncryptionUtil.md5(password))
                        .save();
                setCookie(Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30*24*60*60);
                setSessionAttr(Constants.USER_SESSION, user);
                success();
            } else {
                error("邮箱已经注册过");
            }
        }
    }

    public void productComments(){
        int pageNumber  =  getParaToInt("pageNumber");
        Page<ProdComment> prodCommentPage = ProdComment.prodComment.findAllProductComment(pageNumber,10);
        renderJson(prodCommentPage.getList());
    }


    //进入瀑布流
    public void waterFall(){
        render("huihuo/waterfall.html");
    }

    /**
     * 登出
     */
    public void logout() {
        removeCookie(Constants.USER_COOKIE);
        removeSessionAttr(Constants.USER_SESSION);
        redirect(Constants.getBaseUrl() + "/");
    }




}
